﻿using System;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Presentation.Events;
using StockholmsStad.Windows.UI.Modules.Maintenance.Events;
using StockholmsStad.Windows.UI.Modules.Maintenance.Interfaces;
using StockholmsStad.Windows.UI.Modules.Maintenance.Models;

namespace StockholmsStad.Windows.UI.Modules.Maintenance.Presenters
{
    public class GenerateTimePresenter
    {
        public event EventHandler<DataEventArgs<GenerateTimePresentationModel>> TimeGenerated = delegate { };
        private readonly IMaintenanceService service;
        private readonly IEventAggregator eventAggregator;
        private readonly SubscriptionToken subscriptionToken;

        public GenerateTimePresenter(IScheduleGenerateTimeView view, IMaintenanceService maintenanceService, IEventAggregator aggregator)
        {
            View = view;
            View.TimeGenerated += ((sender, e) => TimeGenerated(sender, e));

            eventAggregator = aggregator;

            service = maintenanceService;

            var changedTitleEvent = eventAggregator.GetEvent<ScheduleCancelledEvent>();

            if (subscriptionToken != null)
                changedTitleEvent.Unsubscribe(subscriptionToken);

            subscriptionToken = changedTitleEvent.Subscribe(ScheduleCancelledEventHandler, ThreadOption.UIThread, false);

        }

        private void ScheduleCancelledEventHandler(GenerateTimePresentationModel model)
        {
            View.Model = model;
        }

        public IScheduleGenerateTimeView View { get; set; }
    }
}
